package com.bugbuff.sso.controller;

import com.bugbuff.common.pojo.User;
import com.bugbuff.common.vo.ErrorResult;
import com.bugbuff.sso.service.SmsService;
import com.bugbuff.sso.service.UserService;
import com.bugbuff.sso.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("user")
@Slf4j
public class UserController {

    @Autowired
    private SmsService smsService;

    @Autowired
    private UserService userService;


    /**
     * {"phone":"17602026868"}
     */
    /*@PostMapping("login")
    public ResponseEntity<ErrorResult> login(@RequestBody Map<String, String> map) {
        log.info("前端传入的参数：{}", map.get("phone"));

        String phone = map.get("phone");
        ErrorResult result = userService.login(phone);
//        ErrorResult result = smsService.sendCheckCode(phone);

        if (result == null) {
            return ResponseEntity.ok(null);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
    }*/

    /**
     * 登录-验证码校验
     */
    @PostMapping("loginVerification")
    public ResponseEntity<Object> loginVerification(@RequestBody Map<String, String> map) {
        String phone = map.get("phone");
        String code = map.get("verificationCode");
        UserVo result = userService.loginVerification(phone, code);

        if (result != null) {
            return ResponseEntity.ok(result);
        }

        ErrorResult errorResult = ErrorResult.builder().errCode("000002").errMessage("验证码不一致！").build();
        return ResponseEntity.status(401).body(errorResult);
    }

    /**
     * 头像上传实现
     */
    /*@PostMapping("loginReginfo/head")
    public void head(@RequestHeader("Authorization") String token,@RequestParam("file") MultipartFile headPhoto){
        System.out.println(token);
        System.out.println(headPhoto.getSize());
    }*/

    /**
     * 提供一个外接访问的token查询
     */
    @GetMapping("{token}")
    public User getToken(@PathVariable("token") String token) {
        return userService.queryUserByToken(token);
    }
}
